<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use think\Model;

class Keywords extends Model{
    protected $autoWriteTimestamp = true;
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filedÍ
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='create_time desc',$limit=10,$filed='*'){
        $list = $this
            ->field($filed)
//            ->with(['user'=>function($query){
//                $query->field('id,account,nickname');
//            }])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        return $list;
    }

    public function user(){
        return $this->hasOne('member','id','mid');
    }

    /**
     * @param string $keywords
     * @param int $type
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addNew($keywords='',$type=1){
        $data = [
            'keywords'=>$keywords,
            'type'=> $type
        ];
        $info = $this
            ->whereRaw("MATCH(keywords) AGAINST ('+{$keywords}' IN BOOLEAN MODE)")
            ->find();
        if($info){
            $info->hits = $info['hits'] +1;
            $result = $info->save();
        }else{
            $result = $this->save($data);
        }
        return $result;
    }
}